package com.sxbbc.common.core.service.system.config;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.basics.service.redis.RedisUtilsService;
import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.ActivitySeckillDetail;
import com.sxbbc.common.core.entity.jz.InfoAli;
import com.sxbbc.common.core.entity.jz.InfoWx;
import com.sxbbc.common.core.entity.jz.OrderConfigGroup;
import com.sxbbc.common.core.enums.GroupStatusEnums;
import com.sxbbc.common.core.enums.redis.RedisExpiredEnums;
import com.sxbbc.common.core.service.IActivitySeckillDetailService;
import com.sxbbc.common.core.service.IInfoAliService;
import com.sxbbc.common.core.service.IInfoWxService;
import com.sxbbc.common.core.service.IOrderConfigGroupService;
import com.sxbbc.common.core.service.system.redis.RedisSeckillService;
import com.sxbbc.common.core.utils.RedisUtils;

/**
 * 初始化加载
 * @author arno
 *
 */
@Component
public class ConfigCore implements ApplicationListener<ContextRefreshedEvent> {
	@Autowired
	private IInfoWxService iInfoWxService;
	@Autowired
	private RedisUtilsService redisUtilsService;
	@Autowired
	private IOrderConfigGroupService orderConfigGroupService;
	@Autowired
	private IActivitySeckillDetailService iActivitySeckillDetailService; 
	@Autowired
	private RedisSeckillService redisSeckillService;
	@Autowired
	private IInfoAliService iInfoAliService;

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if(event.getApplicationContext().getParent() == null){
			QueryWrapper<InfoWx> wrapper=new QueryWrapper<InfoWx>();
			InfoWx infos=iInfoWxService.getOne(wrapper);
			if(infos!=null) {
				//设置微信基础信息
				RedisUtils.getInstance().setWxInfo(infos);
				RedisUtils.getInstance().setWxInfo(infos);
				//设置微信临时access_token
				long expire = redisUtilsService.getExpire(RedisExpiredEnums.WX_WEB_TOKEN.getKey(), TimeUnit.SECONDS);
				if(expire <= 0 && expire != -1) {
					RedisUtils.getInstance().setWxToken(infos);
				}
				//设置微信JS接口的临时票据
				expire = redisUtilsService.getExpire(RedisExpiredEnums.WX_JSAPI_TICKET.getKey(), TimeUnit.SECONDS);
				if(expire <= 0 && expire != -1) {
					String token = RedisUtils.getInstance().getWxToken();
					RedisUtils.getInstance().setWxJsApiTicket(token);
				}
				//设置小程序临时access_token
				expire =  redisUtilsService.getExpire(RedisExpiredEnums.WX_PROGRAM_TOKEN.getKey(), TimeUnit.SECONDS);
				if(expire <= 0 && expire != -1) {
					RedisUtils.getInstance().setWxProgramToken(infos);
				}
			}
			
			//设置支付宝信息
			QueryWrapper<InfoAli> wrapper3=new QueryWrapper<InfoAli>();
			InfoAli infoAli=iInfoAliService.getOne(wrapper3);
			if(infos!=null) {
				//设置微信基础信息
				RedisUtils.getInstance().setAliInfo(infoAli);
			}
			//初始化 将团购 正在秒杀的活动设置进去
			//1.团购  查询正在拼团的 团长订单 
			QueryWrapper<OrderConfigGroup> wrapper2=new QueryWrapper<>();
			wrapper2.eq("status", GroupStatusEnums.GROUP_ING.getId());
			wrapper2.eq("leader", StaticUtils.STATUS_YES);
			wrapper2.gt("over_time", DateUtil.getInstance().currentDate());
			wrapper2.isNotNull("over_time");
			List<OrderConfigGroup> list=orderConfigGroupService.list(wrapper2);
			for(OrderConfigGroup l:list) {
				//计算截止时间到目前相隔多少秒
				long second=(l.getOverTime().getTime()- DateUtil.getInstance().currentTimeMillis())/1000;
				redisUtilsService.setKey(RedisExpiredEnums.GROUP_ORDER.getKey()+l.getGroupCode(), JSONObject.toJSONString(l),second);
			}
			//2.设置正在秒杀中的产品
			List<ActivitySeckillDetail> details=iActivitySeckillDetailService.queryIng();
			for(ActivitySeckillDetail d:details) {
				redisSeckillService.setSkDetail(d);
			}

			System.out.println("核心公共层加载配置成功。。。");
		}
	}
}
